package com.ozo.boot.common.file.strategy;

import cn.hutool.core.lang.UUID;
import com.ozo.boot.common.file.constant.FileStoreTypeEnum;
import com.ozo.boot.common.file.config.FileStoreProperties;
import com.ozo.boot.common.file.config.StrategyFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 本地策略实现
 *
 * @author qiangesoft
 * @date 2024-05-13
 */
@Slf4j
@RequiredArgsConstructor
public class LocalStrategyImpl implements FileStrategy {

    private String prefix;

    public LocalStrategyImpl(FileStoreProperties fileStoreProperties) {
        this.prefix = fileStoreProperties.getPrefix();
    }

    @Override
    public boolean existsCatalog(String catalog) throws Exception {
        File file = new File(this.prefix + SLASH + catalog);
        return file.exists();
    }

    @Override
    public void createCatalog(String catalog) throws Exception {
        File file = new File(this.prefix + SLASH + catalog);
        if (!file.exists()) {
            log.info("The directory {} does not exist to start creating!", catalog);
            file.mkdirs();
        }
    }

    @Override
    public List<String> listCatalogs() throws Exception {
        File file = new File(this.prefix);
        File[] files = file.listFiles();
        List<String> list = new ArrayList<>();
        for (File subFile : files) {
            if (subFile.isDirectory()) {
                list.add(subFile.getName());
            }
        }
        return list;
    }

    @Override
    public void removeCatalog(String catalog) throws Exception {
        File file = new File(this.prefix + SLASH + catalog);
        if (file.exists()) {
            file.delete();
        }
    }

    @Override
    public String uploadFile(String catalog, MultipartFile file) throws Exception {
        // 参数校验
        Assert.notNull(catalog, "catalog is not blank");
        Assert.notNull(file, "file is not null");

        // 判断目录是否存在
        this.createCatalog(catalog);

        // 文件名
        String originalFilename = file.getOriginalFilename();
        Assert.notNull(originalFilename, "originalFilename is not null");
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(POINT));
        String fileName = UUID.randomUUID() + suffix;

        // 上传
        String filePath = this.prefix + File.separator + catalog + File.separator + fileName;
        File targetFile = new File(filePath);
        file.transferTo(targetFile);

        return catalog + SLASH + fileName;
    }

    @Override
    public void removeFile(String catalog, String fileName) throws Exception {
        File file = new File(this.prefix + SLASH + catalog + SLASH + fileName);
        if (file.exists()) {
            file.delete();
        }
    }

    @Override
    public void removeFiles(String catalog, List<String> fileNames) throws Exception {
        for (String fileName : fileNames) {
            File file = new File(this.prefix + SLASH + catalog + SLASH + fileName);
            if (file.exists()) {
                file.delete();
            }
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        StrategyFactory.register(FileStoreTypeEnum.LOCAL, this);
    }
}